export default class TransportMissonExtension extends Room {

    // 虫卵填充任务
    public Task_Feed():void{
        /* 每11 tick 观察一次 */
        if ((global.Gtime[this.name]- Game.time) % 11) return
        if (!this.memory.StructureIdData.storageID) return
        if (this.RoleMissonNum('transport','虫卵填充') < 1)
        {
            var emptyExtensions = new RoomPosition(this.memory.center_x,this.memory.center_y,this.name).findClosestByRange(FIND_MY_STRUCTURES,{filter:(structure)=>{
                return (structure.structureType == 'spawn' || structure.structureType == 'extension') && structure.store.getFreeCapacity('energy') > 0
            }})
            if (emptyExtensions)
            {
                /* 满足条件则触发虫卵填充任务 */
                var thisMisson:MissionModel = {
                    name: "虫卵填充",
                    range:"Creep",
                    delayTick:50,
                    cooldownTick:4,
                    CreepBind:{'transport':{num:1,bind:[]}},
                    data:{}
                }
                this.AddMisson(thisMisson)
            }
        }
        

        
    }

    // 建筑建造任务 有construsion就生产专用爬 五级前常驻，五级后不再常驻
    public Task_Build():void{
        if ((global.Gtime[this.name]- Game.time) % 51) return
        if (this.controller.level < 5) return
        var myConstrusion = new RoomPosition(this.memory.center_x,this.memory.center_y,this.name).findClosestByRange(FIND_MY_CONSTRUCTION_SITES)
        if (myConstrusion)
        global.CreepNumData[this.name]['build'].num = 1
    }

    // 实验室能量填充任务
    public Task_LabEnergy():void{
        if ((global.Gtime[this.name]- Game.time) % 13) return
        if (!this.memory.StructureIdData.storageID) return
        if (!this.memory.StructureIdData.labs || this.memory.StructureIdData.labs.length <= 0)return
        if (this.RoleMissonNum('transport','物流运输') >= 1) return
        for (var id of this.memory.StructureIdData.labs)
        {
            var thisLab = Game.getObjectById(id) as StructureLab
            if (!thisLab)
            {
                var index = this.memory.StructureIdData.labs.indexOf(id)
                this.memory.StructureIdData.labs.splice(index,1)
                continue
            }
            if (thisLab.store.getUsedCapacity('energy') <= 800)
            {
                /* 下布搬运命令 */
                var storage_ = Game.getObjectById(this.memory.StructureIdData.storageID) as StructureStorage
                if (!storage_) return
                if (storage_.store.getUsedCapacity('energy') < 10000)return
                var thisTask = this.Public_Carry({'transport':{num:1,bind:[]}},25,this.name,storage_.pos.x,storage_.pos.y,this.name,thisLab.pos.x,thisLab.pos.y,'energy',2000 - thisLab.store.getUsedCapacity('energy'))
                this.AddMisson(thisTask)
                return
            }
            /* 如果该实验室不在绑定状态却有多余资源 */
            if (!this.memory.RoomLabBind[id] && thisLab.mineralType)
            {
                var storage_ = Game.getObjectById(this.memory.StructureIdData.storageID) as StructureStorage
                if (!storage_) return
                var thisTask = this.Public_Carry({'transport':{num:1,bind:[]}},25,this.name,thisLab.pos.x,thisLab.pos.y,this.name,storage_.pos.x,storage_.pos.y,thisLab.mineralType,thisLab.store.getUsedCapacity(thisLab.mineralType))
                this.AddMisson(thisTask)
                return
            }
        }
    }

    // 远塔填充任务
    public Task_OTower():void{
        if ((global.Gtime[this.name]- Game.time) % 15) return
        if (!this.memory.StructureIdData.storageID) return
        if (!this.memory.StructureIdData.OtowerID || this.memory.StructureIdData.OtowerID.length <= 0)return
        if (this.RoleMissonNum('transport','物流运输') >= 1) return
        for (var id of this.memory.StructureIdData.OtowerID)
        {
            var thisTower = Game.getObjectById(id) as StructureLab
            if (!thisTower)
            {
                var index = this.memory.StructureIdData.OtowerID.indexOf(id)
                this.memory.StructureIdData.OtowerID.splice(index,1)
                continue
            }
            if (thisTower.store.getUsedCapacity('energy') <= 600)
            {
            /* 下布搬运命令 */
            var storage_ = Game.getObjectById(this.memory.StructureIdData.storageID) as StructureStorage
            if (!storage_) return
            if (storage_.store.getUsedCapacity('energy') < 10000)return
            var thisTask = this.Public_Carry({'transport':{num:1,bind:[]}},25,this.name,storage_.pos.x,storage_.pos.y,this.name,thisTower.pos.x,thisTower.pos.y,'energy',1000 - thisTower.store.getUsedCapacity('energy'))
            this.AddMisson(thisTask)
            return
            }
        }
    }

    // 核弹填充任务
    public Task_Nuker():void{
        if (Game.time % 103) return
        if (!this.memory.StructureIdData.NukerID || !this.memory.StructureIdData.storageID) return
        if (this.RoleMissonNum('transport','物流运输') >= 1) return
        var nuker = Game.getObjectById(this.memory.StructureIdData.NukerID) as StructureNuker
        var storage_ = Game.getObjectById(this.memory.StructureIdData.storageID) as StructureStorage
        if (!nuker) {delete this.memory.StructureIdData.NukerID;return}
        if (!storage_){delete this.memory.StructureIdData.storageID;return}
        if (nuker.store.getUsedCapacity('G') < 5000 && storage_.store.getUsedCapacity('G') > 5000)
        {
            var thisTask = this.Public_Carry({'transport':{num:1,bind:[]}},40,this.name,storage_.pos.x,storage_.pos.y,this.name,nuker.pos.x,nuker.pos.y,'G',5000 - nuker.store.getUsedCapacity('G'))
            this.AddMisson(thisTask)
            return
        }
        if (nuker.store.getUsedCapacity('energy') < 300000 && storage_.store.getUsedCapacity('energy') > 160000)
        {
            var thisTask = this.Public_Carry({'transport':{num:1,bind:[]}},40,this.name,storage_.pos.x,storage_.pos.y,this.name,nuker.pos.x,nuker.pos.y,'energy',300000 - nuker.store.getUsedCapacity('energy'))
            this.AddMisson(thisTask)
            return
        }
    }
}